using SharpDX;
using SharpDX.Direct3D11;
using System;

namespace Engine.Graphics.Shaders.C11UndercoverShaders
{
    internal class Material000 : ShaderBaseC11Undercover, ISetShaderValues
    {
        #region Private Fields

        private BlendState blendstate;
        private int lastBumpTextureID1 = -1;
        private int lastBumpTextureID2 = -1;
        private int lastBumpTextureID3 = -1;
        private uint lastHashcodeMappingMatrix1 = 0;
        private uint lastHashcodeMappingMatrix2 = 0;
        private uint lastHashcodeMappingMatrix3 = 0;
        private int lastTextureID1 = -1;
        private int lastTextureID2 = -1;
        private int lastTextureID3 = -1;
        private string name;
        private SamplerState samplerState;
        private SamplerStateDescription samplerStateDescription;

        #endregion Private Fields

        #region Public Constructors

        public Material000(Device renderDevice, string shaderDirectory)
            : base(renderDevice)
        {
            var blendDesc = new BlendStateDescription();
            blendDesc.AlphaToCoverageEnable = false;
            blendDesc.IndependentBlendEnable = false;
            blendDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendDesc.RenderTarget[0].DestinationBlend = BlendOption.Zero;
            blendDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
            blendDesc.RenderTarget[0].SourceBlend = BlendOption.One;
            blendDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
            blendDesc.RenderTarget[0].IsBlendEnabled = false;
            blendDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;

            this.blendstate = new BlendState(renderDevice, blendDesc);

            this.name = shaderDirectory;

            this.name = shaderDirectory;
            base.LoadShader(renderDevice, shaderDirectory + "\\material00.fx", "ps_main", "vs_main");
            base.inputLayout = new InputLayout(renderDevice, base.shaderByteCodeVS, Graphics.VertexStructures.MyOwnVertex.MyPositionNormalTextured.elements);
            base.inputLayout.DebugName = "Input Layout Material00";

            // Create samplerstate
            samplerStateDescription = new SamplerStateDescription()
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = new Color4(0),
                ComparisonFunction = Comparison.Always,
                Filter = Filter.Anisotropic,
                MaximumAnisotropy = 16,
                MaximumLod = float.MaxValue,
                MinimumLod = 0,
                MipLodBias = 0
            };

            samplerState = new SamplerState(renderDevice, samplerStateDescription);
            samplerState.DebugName = "SamplerState Material00";
        }

        #endregion Public Constructors

        #region Public Properties

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion Public Properties

        #region Public Methods

        public new void Dispose()
        {
            base.Dispose();
        }

        public new void SetProperties(int materialID, MaterialManager materialManager)
        {
            //this.device.ImmediateContext.OutputMerger.SetBlendState(this.blendstate, sampleMask: 255);

            Material material = materialManager.GetMaterial(materialID);

            short TextureID1 = materialManager.GetTextureIDTexture1(materialID);
            short TextureID2 = materialManager.GetTextureIDTexture2(materialID);
            short TextureID3 = materialManager.GetTextureIDTexture3(materialID);
            short BumpTextureID1 = materialManager.GetTextureIDBumbTexture1(materialID);
            short BumpTextureID2 = materialManager.GetTextureIDBumbTexture2(materialID);
            short BumpTextureID3 = materialManager.GetTextureIDBumbTexture3(materialID);

            this.lastTextureID1 = -1;
            this.lastTextureID2 = -1;
            this.lastTextureID3 = -1;
            this.lastBumpTextureID1 = -1;
            this.lastBumpTextureID2 = -1;
            this.lastBumpTextureID3 = -1;

            base.SetSamplerState(samplerState, 0);
            base.SetSamplerState(null, 1);
            base.SetSamplerState(samplerState, 2);
            base.SetSamplerState(null, 3);
            base.SetSamplerState(samplerState, 4);
            base.SetSamplerState(null, 5);
            base.SetTextureUvMatrix(material);

            //effect.SetValue("paintHighLines", false);
            if (this.lastTextureID1 != TextureID1)
            {
                this.lastTextureID1 = TextureID1;

                base.SetTexture(0, material.GetTexture1(materialManager));
                base.SetTexture(1, material.GetBumpTexture1(materialManager));
            }

            if (this.lastTextureID2 != TextureID2)
            {
                this.lastTextureID2 = TextureID2;

                base.SetTexture(2, material.GetTexture2(materialManager));
                base.SetTexture(3, material.GetBumpTexture2(materialManager));
            }
            if (this.lastTextureID3 != 3)
            {
                this.lastTextureID3 = TextureID3;

                base.SetTexture(4, material.GetTexture3(materialManager));
                base.SetTexture(5, material.GetBumpTexture3(materialManager));
            }
        }

        #endregion Public Methods
    }
}